
package protocol.transactions;

import protocol.XoneCallBack;
import protocol.XoneProtocol;
import protocol.XoneService;
import android.text.TextUtils;

import com.netease.log.NTLog;

abstract public class EncryptBaseTransaction extends XoneBaseTransaction {

    public EncryptBaseTransaction(int type) {
        super(type);

    }

    @Override
    public void onTransact() {
        if (!TextUtils.isEmpty(XoneProtocol.getInstance().getIntitId())
                && !TextUtils.isEmpty(XoneProtocol.getInstance().getEncryptedKey())) {
            realOntransact();
        } else {
            XoneService.getInstance().addListener(mGetIdKeyCallback);
            XoneService.getInstance().doGetIdKey();
        }
    }

    private XoneCallBack mGetIdKeyCallback = new XoneCallBack() {

        @Override
        public void onGetIdKey(int transactionId) {
            NTLog.i("EncryptBaseTransaction", "onGetIdKey");
            getTransactionEngine().beginTransaction(EncryptBaseTransaction.this);
            XoneService.getInstance().removeListener(mGetIdKeyCallback);
        };

        @Override
        public void onGetIdKeyError(int transactionId, int errCode, String err) {
            NTLog.i("EncryptBaseTransaction", "onGetIdKeyError");
            notifyError(errCode, err);
            XoneService.getInstance().removeListener(mGetIdKeyCallback);
            EncryptBaseTransaction.this.doEnd();
        };
    };

    abstract void realOntransact();
}
